<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/device' }"
        >设备管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/device/trashcan/location' }"
        >垃圾箱位置
      </el-breadcrumb-item>
      <el-breadcrumb-item>绑定设备</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form :model="form" :rules="rules" label-width="120px" ref="form">
          <el-form-item class="m-input-400" label="imei" prop="imei">
            <el-input autocomplete="off" v-model="form.imei" />
          </el-form-item>
          <el-form-item class="m-input-400" label="位置名称">
            {{ form.locationName }}
          </el-form-item>
          <el-form-item class="m-input-400" label="垃圾箱类型">
            {{ form.trashcanType }}
          </el-form-item>
          <el-form-item class="m-input-400" label="垃圾箱容量">
            {{ form.volume }}L
          </el-form-item>
          <el-form-item class="m-input-400" label="垃圾箱经度">
            {{ form.lng }}
          </el-form-item>
          <el-form-item class="m-input-400" label="垃圾箱纬度">
            {{ form.lat }}
          </el-form-item>
          <el-form-item class="m-input-400" label="备注">
            {{ form.remarks }}
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即绑定
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {
        imei: null
      },
      rules: {
        imei: [{ required: true, message: "请输入设备imei", trigger: "none" }]
      }
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.info();
  },
  methods: {
    info() {
      this.$ajax.trashcan_location
        .info(this.id)
        .then(result => {
          if (result.code == 1) {
            this.form = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.trashcan_location
            .bind({
              id: this.id,
              imei: this.form.imei
            })
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "绑定垃圾箱位置成功!"
                });
                this.$router.push("/device/trashcan/location");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/device/trashcan/location");
    }
  }
};
</script>
